package com.itheima.service.impl;

import com.itheima.dao.FileDao;
import com.itheima.pojo.File;
import com.itheima.service.FileService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.utils.QiniuUtils;
import com.itheima.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private FileDao fileDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * 通过fileIds过去信息接口
     *
     * @param fileIds
     * @param code
     * @return
     */
    @Override
    public List<File> getPicture(String fileIds, String code) {
        if (fileIds == null || "".equals(fileIds)) {
            throw new RuntimeException("没有传入文件id");
        }
        List<File> list = new ArrayList<>();
        String[] split = fileIds.split(code);
        if (split.length == 0) {
            File file = fileDao.selectByPrimaryKey(fileIds);
            list.add(file);
        } else {
            for (String s : split) {
                File file = fileDao.selectByPrimaryKey(s);
                list.add(file);
            }
        }
        return list;
    }

    /**
     * 多图片上传
     *
     * @param request
     * @param filedatas
     * @return
     */
    @Override
    public List<File> pictures(HttpServletRequest request, List<MultipartFile> filedatas) {
        //获取上传图片集合
        List<File> files = new ArrayList<>();
        for (MultipartFile filedata : filedatas) {
            File file = new File();
            //获取上传时的文件名
            String fileOldName = filedata.getOriginalFilename();
            file.setFileOldName(fileOldName);

            //通过"."来切割 获取图片扩展名
            String picExpandedName = fileOldName.substring(fileOldName.indexOf(".") + 1);
            file.setPicExpandedName(picExpandedName);

            //生成Uid
            String uid=idWorker.nextId()+"";
            file.setUid(uid);

            //生成唯一图片名
            String fileName=uid+"."+picExpandedName;
            file.setPicName(fileName);

            file.setFileSize(filedata.getSize());

            file.setCreateTime(DateUtils.getNowTime());
            //设置图片路径
            String picUrl="http://shizhan4g.s3-cn-south-1.qiniucs.com/"+fileName;
            file.setQiNiuUrl(picUrl);
            file.setPicUrl(picUrl);

           //需要在cookie中获取
            file.setAdminUid("test123");
            file.setUserUid("test123");


            file.setStatus(1);
            file.setCreateTime(DateUtils.getNowTime());
            file.setUpdateTime(DateUtils.getNowTime());
            //将文件保存到七牛云上
            try {
                QiniuUtils.upload2Qiniu(filedata.getBytes(),fileName);
                //获取成功后的所有图片信息返回
                files.add(file);
                fileDao.insert(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return files;
    }
}
